﻿#define _CRT_SECURE_NO_WARNINGS 1
#include"snake.h"

//设置光标位置
void SetPos(int x, int y)
{
	COORD pos = { x, y };
	HANDLE houtput = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(houtput, pos);
}

void color(int c)
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c); //颜色设置
	//注：SetConsoleTextAttribute是一个API（应用程序编程接口）
}

//打印欢迎界面
void WelcomeToGame()
{
	color(7);
	SetPos(40, 15);
	wprintf(L"欢迎来到贪吃蛇小游戏\n");
	SetPos(40, 25);
	system("pause");
	system("cls");
	SetPos(25, 12);
	wprintf(L"用↑ . ↓ . ← . → 分别控制蛇的移动，F3为加速，F4为减速\n");
	SetPos(25, 13);
	wprintf(L"加速能得到更高的分数\n");
	SetPos(40, 25);
	system("pause");
	system("cls");
}

//创建地图
void GreatMap()
{
	color(6);
	SetPos(0, 0);
	//上
	for (int i = 0; i < 29; i++)
	{
		wprintf(L"%lc", WALL);
	}
	//下
	SetPos(0, 26);
	for (int i = 0; i < 29; i++)
	{
		wprintf(L"%lc", WALL);
	}
	//左
	for (int i = 1; i < 26; i++)
	{
		SetPos(0, i);
		wprintf(L"%lc", WALL);
	}
	//右
	for (int i = 1; i < 26; i++)
	{
		SetPos(56, i);
		wprintf(L"%lc", WALL);
	}
}

//初始化蛇身
void InitSnake(pSnake ps)
{
	pSnakeNode cur = NULL;
	color(10);
	for (int i = 0; i < 5; i++)
	{
		//头插建立蛇身节点
		cur = (pSnakeNode)malloc(sizeof(SnakeNode));
		if (cur == NULL)
		{
			perror("InitSnake():: malloc");
			exit(1);
		}
		//设置cur节点坐标
		cur->next = NULL;
		cur->x = POS_X + 2 * i;
		cur->y = POS_Y;
		//头插
		if (ps->_pSnake == NULL)
		{
			ps->_pSnake = cur;
		}
		else
		{
			cur->next = ps->_pSnake;
			ps->_pSnake = cur;
		}
	}
	//打印蛇的身体
	cur = ps->_pSnake;
	while (cur)
	{
		if (cur == ps->_pSnake)
		{
			SetPos(cur->x, cur->y);
			wprintf(L"%lc", BODY2);
		}
		else
		{
			SetPos(cur->x, cur->y);
			wprintf(L"%lc", BODY1);
		}
		cur = cur->next;
	}
	//设置贪吃蛇属性
	ps->_Dir = RIGHT;
	ps->_Score = 0;
	ps->_foodWeight = 10;
	ps->_Status = OK;
	ps->_SleepTime = 200;
}

//创建食物
void CreatFood(pSnake ps)
{
	int x = 0;
	int y = 0;
again:
	do
	{
		x = rand() % 53 + 2;
		y = rand() % 25 + 1;
	} while (x % 2); //x必须是2的倍数
	//x和y不能和蛇身节点坐标冲突
	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		if (cur->x == x && cur->y == y)
		{
			goto again;
		}
		cur = cur->next;
	}
	//创建食物
	pSnakeNode pFood = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pFood == NULL)
	{
		perror("CreatFood:: malloc");
		exit(1);
	}
	color(14);
	pFood->x = x;
	pFood->y = y;
	pFood->next = NULL;
	SetPos(pFood->x, pFood->y);
	wprintf(L"%lc", FOOD);
	ps->_pFood = pFood;
}

void PrintHelpInfo()
{
	//打印提⽰信息
	color(15);
	SetPos(64, 15);
	wprintf(L"%ls",L"不能穿墙，不能咬到自己\n");
	SetPos(64, 16);
	wprintf(L"%ls",L"用↑.↓.←.→分别控制蛇的移动.");
	SetPos(64, 17);
	wprintf(L"%ls", L"F3 为加速，F4 为减速\n");
	SetPos(64, 18);
	wprintf(L"%ls", L"ESC ：退出游戏.    space：暂停游戏.");
	SetPos(70, 20);
	wprintf(L"%ls", L"广工制作");
}

void pause()
{
	while (1)
	{
		Sleep(300);
		if (KEY_PRESS(VK_SPACE))
		{
			break;
		}
	}
}

int NextIsFood(pSnakeNode pNextNode, pSnake ps)
{
	return (pNextNode->x == ps->_pFood->x  && pNextNode->y == ps->_pFood->y);
}

void EatFood(pSnakeNode pNextNode, pSnake ps)
{
	color(10);
	//头插
	ps->_pFood->next = ps->_pSnake;
	ps->_pSnake = ps->_pFood;
	//释放下一位置的个节点
	free(pNextNode);
	pNextNode = NULL;
	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		if (cur == ps->_pSnake)
		{
			SetPos(cur->x, cur->y);
			wprintf(L"%lc", BODY2);
		}
		else
		{
			SetPos(cur->x, cur->y);
			wprintf(L"%lc", BODY1);
		}
		cur = cur->next;
	}
	ps->_Score += ps->_foodWeight;
	//free(ps->_pFood);
	CreatFood(ps);
}

void NoFood(pSnakeNode pNextNode, pSnake ps)
{
	color(10);
	//头插法
	pNextNode->next = ps->_pSnake;
	ps->_pSnake = pNextNode;
	pSnakeNode cur = ps->_pSnake;
	while (cur->next->next)
	{
		if (cur == ps->_pSnake)
		{
			SetPos(cur->x, cur->y);
			wprintf(L"%lc", BODY2);
		}
		else
		{
			SetPos(cur->x, cur->y);
			wprintf(L"%lc", BODY1);
		}
		cur = cur->next;
	}
	//把最后一个节点打印成空格
	SetPos(cur->next->x, cur->next->y);
	printf("  ");
	//释放最后一个节点
	free(cur->next);
	cur->next = NULL;
}

void killByWall(pSnake ps)
{
	if ((ps->_pSnake->x == 0)
		|| (ps->_pSnake->x == 56)
		|| (ps->_pSnake->y == 0)
		|| (ps->_pSnake->y == 26))
	{
		ps->_Status = KILL_BY_WALL;
	}
}

void killBySelf(pSnake ps)
{
	pSnakeNode cur = ps->_pSnake->next;
	while (cur)
	{
		if (cur->x == ps->_pSnake->x && cur->y == ps->_pSnake->y)
		{
			ps->_Status = KILL_BY_SELF;
			break;
		}
		cur = cur->next;
	}
}

void SnakeMove(pSnake ps)
{
	//创建下一个节点
	pSnakeNode pNextNode = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pNextNode == NULL)
	{
		perror("SnakeMove()::malloc()");
		exit(1);
	}
	switch (ps->_Dir)
	{
	case UP:
		pNextNode->x = ps->_pSnake->x;
		pNextNode->y = ps->_pSnake->y - 1;
		break;
	case DOWN:
		pNextNode->x = ps->_pSnake->x;
		pNextNode->y = ps->_pSnake->y + 1;
		break;
	case LEFT:
		pNextNode->x = ps->_pSnake->x - 2;
		pNextNode->y = ps->_pSnake->y;
		break;
	case RIGHT:
		pNextNode->x = ps->_pSnake->x + 2;
		pNextNode->y = ps->_pSnake->y;
		break;
	}
	if (NextIsFood(pNextNode, ps))
	{
		EatFood(pNextNode, ps);
	}
	else
	{
		NoFood(pNextNode, ps);
	}
	//检查撞墙
	killByWall(ps);
	//检查自杀
	killBySelf(ps);
}

//游戏开始
void GameStart(pSnake ps)
{
	//1、控制台大小设置
	system("mode con cols=100 lines=30");
	//2、控制台窗口名字设置
	system("title 贪吃蛇");
	//3、鼠标隐藏
	HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	//影藏光标操作
	CONSOLE_CURSOR_INFO CursorInfo;
	GetConsoleCursorInfo(hOutput, &CursorInfo);//获取控制台光标信息
	CursorInfo.bVisible = false; //隐藏控制台光标
	SetConsoleCursorInfo(hOutput, &CursorInfo);//设置控制台光标状态
	//4、打印欢迎界面
	WelcomeToGame();
	//5、创建地图
	GreatMap();
	//6、初始化蛇
	InitSnake(ps);
	//7、创建食物
	CreatFood(ps);
}
 
//游戏运行
void GameRun(pSnake ps)
{
	PrintHelpInfo();
	do
	{   
		color(4);
		SetPos(64, 10);
		printf("得分：%d ", ps->_Score);
		SetPos(64, 11);
		printf("每个食物得分：%2d分", ps->_foodWeight);
		if (KEY_PRESS(VK_UP) && ps->_Dir != DOWN)
		{
			ps->_Dir = UP;
		}
		else if (KEY_PRESS(VK_DOWN) && ps->_Dir != UP)
		{
			ps->_Dir = DOWN;
		}
		else if (KEY_PRESS(VK_LEFT) && ps->_Dir != RIGHT)
		{
			ps->_Dir = LEFT;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->_Dir != LEFT)
		{
			ps->_Dir = RIGHT;
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			pause();
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			ps->_Status = END_NOMAL;
			break;
		}
		else if (KEY_PRESS(VK_F3))
		{
			if (ps->_SleepTime >= 80)
			{
				ps->_SleepTime -= 30;
				ps->_foodWeight += 2;//⼀个⻝物分数最⾼是20分
			}
		}
		else if (KEY_PRESS(VK_F4))
		{
			if (ps->_foodWeight > 2)
			{
				ps->_SleepTime += 30;
				ps->_foodWeight -= 2;
			}
		}
		SnakeMove(ps);
		Sleep(ps->_SleepTime);
		
	} while (ps->_Status == OK);
}

//游戏结束
void GameEnd(pSnake ps)
{
	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		pSnakeNode del = cur;
		cur = cur->next;
		free(del);
	}
	color(4);
	SetPos(24, 12);
	switch (ps->_Status)
	{
	case END_NOMAL:
		wprintf(L"您主动退出游戏\n");	
		break;
	case KILL_BY_WALL:
		wprintf(L"您撞墙了,游戏结束!\n");
		break;
	case KILL_BY_SELF:
		wprintf(L"您撞上⾃⼰了 ,游戏结束!\n");
		break;
	}
}
